package com.suvel.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("autodeliver")
public class AutoDeliverController {

    @Autowired
    private RestTemplate restTemplate;

    /*@GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId){
        return restTemplate.getForObject("http://localhost:8080/resume/openstate/" + userId,Integer.class);
    }*/

    @Autowired
    private DiscoveryClient discoveryClient;

   /* @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId){
        List<ServiceInstance> instances = discoveryClient.getInstances("LAGOU-SERVICE-RESUME-SELF");
        ServiceInstance serviceInstance = instances.get(0);

        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();

        String url = "http://" + host + ":" + port + "/resume/openstate/" + userId;

        return restTemplate.getForObject(url,Integer.class);
    }*/

    @GetMapping("/checkState/{userId}")
    public Integer findResumeOpenState(@PathVariable Long userId){
        String url = "http://lagou-service-resume-self/resume/openstate/" + userId;
        return restTemplate.getForObject(url,Integer.class);
    }
    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000"),
                    @HystrixProperty(name =
                            "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    @HystrixProperty(name =
                            "circuitBreaker.requestVolumeThreshold",value = "2"),
                    @HystrixProperty(name =
                            "circuitBreaker.errorThresholdPercentage",value = "50"),
                    @HystrixProperty(name =
                            "circuitBreaker.sleepWindowInMilliseconds",value = "3000")},
            fallbackMethod = "myFallBack",
            threadPoolKey = "findResumeOpenStateTimeOut",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize",value = "1"),
                    @HystrixProperty(name = "maxQueueSize",value = "20")
            }
    )
    @GetMapping("/checkStateTimeOut/{userId}")
    public Integer findResumeOpenStateTimeOut(@PathVariable Long userId){


        String url = "http://lagou-service-resume-self/resume/openstate/" + userId;

        return restTemplate.getForObject(url,Integer.class);
    }

    private Integer myFallBack(Long userId){
        return -1;
    }

}
